using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using MyAutoMapper.Model;

namespace MyAutoMapper.Controllers;

/// <summary>
/// 用户管理
/// </summary>
[ApiController]
[Route("[controller]/[action]")]
public class HomeController : ControllerBase {
  private readonly IMapper _mapper;

  public HomeController (IMapper mapper) {
    _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
  }

  [HttpPost]
  [ActionName("dao2dto")]
  public IActionResult Dao2dto (UserDao dao) {
    // var dao = new UserDao
    // {
    //     Name = "Jack",
    //     Email = "jack@gmail.com",
    //     Version = "12"
    // };

    var model = _mapper.Map<UserDto>(dao);
    return Ok(model);
  }

  [HttpPost]
  [ActionName("dto2dao")]
  public IActionResult Dto2dao (UserDto dto) {
    // var dto = new UserDto
    // {
    //     Id = 1001,
    //     Name = "Lily",
    //     Email = "lily@qq.com"
    // };

    var model = _mapper.Map<UserDao>(dto);
    return Ok(model);
  }
}